package xiaye.mycrawler;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class SimpleFrontier implements Frontier{
	private int count = 0;
	private BlockingQueue<Address> queue;
	
	public SimpleFrontier(){
		init();
	}
	
	private void init(){
		queue = new LinkedBlockingQueue<Address>();
		for (int i = 1; i < 10; i++){
			queue.add(new Address(String.valueOf(i)));
		}
	}
	
	public Address getNext() throws FrontierEmptyException{
		try {
			Address next = queue.poll(30, TimeUnit.SECONDS);
			if (next == null){
				throw new FrontierEmptyException();
			}
			return next;
		} catch (InterruptedException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public void processPage(Page page) {
		for (Address address : page.getOutLinks()){
			queue.add(address);
			System.out.println(address);
		}
	}
}
